define([
	"UtilDir/util",
	"UtilDir/tool",
	"UtilDir/combotree",
	"UtilDir/dialog",
    "UtilDir/searchBlock",
	"EasyUI","EasyUI-lang"
    ],function(Util,Tool,Combotree,Dialog,SearchBlock,Theme){

    var sysPath =  getServer() + "/static/app/fwsjcj";

    var auditFormHtml = sysPath + "/useraudit/views/auditForm.html";
	var getZwController = function(){
		return getServer() + "/platform/org/zws/";
	};
	var getDivisionController = function () {
        return getServer() + "/platform/org/divisions/";
    };
	/**
	 * 页面初始化
	 */
	var init = function(){
        initSearchBlock();
        queryBtnBind();
		createAuditGrid();
        //浏览器窗口大小变化后，表格宽度自适应
        $(window).resize(function(){
            createAuditGrid();
        });
//      initSearchForm();
	};
	var initSearchBlock = function(){
        SearchBlock.init("searchBlock");
	};
	var queryBtnBind = function(){
        $("#btnSearch").click(function () {
            createAuditGrid();
        });
        $("#btnReset").click(function () {
            $("#userType").val("");
            $("#relName").val("");
            $("#mobilePhone").val("");
            $("#status").val("");
        });
	};
	var createAuditGrid = function() {
        $("#auditGrid").datagrid({
            url:getServer() + "/fw/user/page",
			method:"GET",
            fitColumns: true,
            nowrap: false,
            autoRowHeight: false,
            columns:[[
				{field:"ck",checkbox:true},
				{field:'relName',title:'姓名',width:'14.7%',align:'center'},
				{field:'idNumber',title:'身份证号',width:'15%',align:'center'},
				{field:'mobilePhone',title:'手机号',width:'10%',align:'center'},
				{field:'userAddress',title:'地区',width:'15%',align:'center'},
//              {field:'zwName',title:'职务名称',width:'20%',align:'center',
//                  formatter:function(value,rowData,rowIndex){
//                      return '<a href="#" onclick="_editZw(\'' + rowData.zwId+ '\');"> '+rowData.zwName+' </a> '
//
//                  }
//              },
                {field:'userType',title:'用户类型',width:'15%',align:'center',
                	formatter: function (value, rowData, rowIndex) {
                        if(value == '0'){
                			return '普查信息采集员';
                		}else if(value == '1'){
                			return '详查信息采集员';
                		}else if(value =='2'){
                            return '加固工程采集员'
                        }else if(value =='3'){
                            return '普查和加固工程采集员'
                        }
                    }
                },
                {field:'status',title:'处理状态',width:'10%',align:'center',
                	formatter: function (value, rowData, rowIndex) {
                        if(value == '0'){
                			return '待审核';
                		}else if(value == '2'){
                			return '审核不通过';
                		}                       		
                    }
                },
                {field:'operation',title:'操作',width:'20%',align:'center',
                	formatter: function (value, rowData, rowIndex) {
                        return '<a onclick="_check(\'' + rowData.id+ '\')">审核通过</a >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a onclick="_uncheck(\'' + rowData.id+ '\')">审核不通过</a >';                        		
                    }
                }
                
            ]],
            toolbar: [
                {
                    iconCls: 'fa fa-plus-circle',
                    text:"批量审核",
                    handler: function(){
                        batchCheck()
                    }
                },
                {
                    iconCls: 'fa fa-plus-circle',
                    text:"批量删除",
                    handler: function(){
                        batchDelete()
                    }
                }
            ],
            queryParams:{
                userType: $("#userType").val(),
                relName: $("#relName").val(),
                mobilePhone: $("#mobilePhone").val(),
                status: $("#status").val()
            },
            pagination: true,
            pageSize: 10
        });
    };



	var formValidator = function(){
		debugger;
		$("#auditForm").validate({
			rules : {
				checkResult : {
					required :true
				}			
			},
			messages : {
                checkResult : {
					required:"审核结果不允许为空!"
				}
			}
		});
	};

	// 批量审核
	var batchCheck = function () {
		var rows = $("#auditGrid").datagrid("getSelections");
		if (rows == null || rows.length == 0) {
			Util.alert("请选择用户!");
			return;
		}
		var ids = "";
		$.each(rows, function(i, row){
			ids += row.id + ",";
		});
		ids = ids.substr(0,ids.length - 1);
		var slidebar = Util.slidebar({
			url: auditFormHtml,
			width: "580px",
			cache: false,
			close : true,
			afterLoad: function () {
				formValidator();
				$("#saveBtn").on("click", function () {
					if($("#auditForm").valid()){
						var checkResult = $("#checkResult").val();
						debugger;
						$.ajax({
							url: getServer() +"/fw/user/"+ids+"/"+checkResult,
                            contentType:"application/json",
//							data: JSON.stringify(data),
							type: "post",
							success: function (data) {
								Util.alert(data.message);
								slidebar.close();
                                createAuditGrid();
							}
						});
					}
				});
			}
		});
	};

	// 批量删除
    var batchDelete = function () {
        var rows = $("#auditGrid").datagrid("getSelections");
        if (rows == null || rows.length == 0) {
            Util.alert("请选择一行数据!");
            return;
        }
        Util.confirm("是否要删除选中的表单?", function () {
            var ids = "";
            $.each(rows, function (i, row) {
                ids += row.id + ",";
            });
            ids = ids.substr(0, ids.length - 1);
            $.ajax({
                url: getServer() +"/fw/user/deleteFwUser",
                contentType: "application/json",
                data: ids,
                type: "post",
                success: function (data) {
                    Util.alert(data.message);
                    createAuditGrid();
                }
            });
        }, function () {
            return;
        });
    }


    window._check = function(ids) {
		console.log(ids);
		$.ajax({
			url: getServer() +"/fw/user/"+ids+"/"+1,
            contentType:"application/json",
			type: "post",
			success: function (data) {
				Util.alert(data.message);
                createAuditGrid();
			}
		});
	};
	window._uncheck = function(ids) {
		$.ajax({
			url: getServer() +"/fw/user/"+ids+"/"+2,
            contentType:"application/json",
			type: "post",
			success: function (data) {
				Util.alert(data.message);
                createAuditGrid();
			}
		});
//      var buttons = [];
//      buttons.push({
//          name: '确定',
//          callback: function () {
//          	debugger;
//          	console.log($("#reason").val())
//              //点击确定
////              $.ajax({
////					url: getZwController() ,
////		            contentType:"application/json",
////					data: id,
////					type: "post",
////					success: function (data) {
////						Util.alert(data.message);
////		                createAuditGrid();
////		                dialog.hide();
////					}
////				});
//              
//          }
//      });
//
//      var dialog = Dialog({
//          id: "uncheckDialog",
//          cache: false,
//          title: '不通过原因', //dialog标题
//          width: "550px",
//          height: "200px",
//          body: '<form id="uncheckForm" class="form-horizontal">'+
//			            '<div class="form-group">'+		            
//				            '<div class="col-md-12">'+
//				                '<textarea rows="7" class="form-control" id="reason" name="reason" />'+
//				            '</div>'+
//				        '</div>'+
//          		'</form>',
//          buttons: buttons,
//          afterOpen: function () {
//              
//          }
//      });
//      dialog.show();
        
	};

	var initSearchForm = function () {
        //初始化行政岗区划
        var combotree = Combotree({
            // combo组件相关配置
            combo: {
                //要渲染的input的id
                id: "division",
                //下拉框的类型：单选or多选
                multi: false,
                key: {
                    idKey: "divisionId",
                    textKey: "divisionName"
                },
                //          placeholder: "请选择……",
                event: {
                    onShowPanel: function () {
                        console.log("show");
                    },
                    onHidePanel: function () {
                        console.log("hide");
                    },
                    onChange: function (n, o) {
                    	debugger;
                        $("#divisionId").val(n[0].divisionId);
                        $("#division").val(n[0].divisionName);
                    }
                }
            },
            // ztree组件相关配置
            ztree: {
                data: {
                    simpleData: {
                        enable: true,
                        idKey: "divisionId", // 主键属性名
                        pIdKey: "parentId" // 父节点属性名
                    },
                    key: {
                        name: "divisionName" // 显示名称属性名
                    }
                },
                check: {
                    chkboxType: {
                        'Y': '',
                        'N': ''
                    }
                }
            },
            data: {
                url: getDivisionController() + 'initDivisionZtree',
                type: 'GET',
                success: function () {

                }
            },
            formatter: function (node) {
                node.open = true;
            },
            value: $("#divisionId").val(),
            search: {
                enable: true,
                fields: ["divisionName"]
            }
        });
   	}
	return {
		init:init
	};
});
